/*
 * File Name：Circle.java
 * Copyright：Copyright 2008-2012 CiWong.Inc. All Rights Reserved.
 * Description： Circle.java
 * Modify By：PLA-ZJLIU
 * Modify Date：2012-12-28
 * Modify Type：Add
 */
package com.ciwong.ciwongwrite.widget;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.BlurMaskFilter;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.MaskFilter;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.util.AttributeSet;
import android.view.View;

import com.ciwong.ciwongwrite.R;

/**
 * <pre>
 * </pre>
 *
 * @author PLA-ZJLIU
 * @version ciwong v.1.0 2012-12-28
 * @since ciwong v.1.0
 */
public class Circle extends View
{
    private int radius;

    private int color;

    // private Drawable selectDrawable;

    private boolean isSelected;

    private MaskFilter mBlur;

    public Circle(Context context)
    {
        super(context);
    }

    public Circle(Context context, AttributeSet attrs)
    {
        super(context, attrs);
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.graph);
        init(context, a);
    }

    public Circle(Context context, AttributeSet attrs, int defStyle)
    {
        super(context, attrs, defStyle);
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.graph);
        init(context, a);
    }

    private void init(Context context, TypedArray a)
    {
        radius = a.getInt(R.styleable.graph_radius, 5);
        color = a.getColor(R.styleable.graph_colors, Color.BLACK);
//        int drawableRes = a.getInt(R.styleable.graph_select_drawable,
//                R.drawable.select_bg);
        isSelected = a.getBoolean(R.styleable.graph_checked, false);
        // selectDrawable = context.getResources().getDrawable(drawableRes);
        a.recycle();
        mBlur = new BlurMaskFilter(radius / 2, BlurMaskFilter.Blur.NORMAL);
    }

    public void setSelected()
    {
        isSelected = true;
        invalidate();
    }

    public void cancelSelected()
    {
        isSelected = false;
        invalidate();
    }

    public boolean isSelected()
    {
        return isSelected;
    }

    public int getPaintWidth()
    {
        return radius;
    }

    @Override
    protected void onDraw(Canvas canvas)
    {
        final Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        if (!isSelected)
        {
            mPaint.setMaskFilter(mBlur);
        }
        else
        {
            mPaint.setMaskFilter(null);
        }
        mPaint.setColor(color);
        mPaint.setStyle(Style.FILL);
        final int w = getMeasuredWidth();
        final int h = getMeasuredHeight();
        final float cx = w / 2;
        final float cy = h / 2;
        canvas.drawCircle(cx, cy, radius, mPaint);

    }
}
